package com.test;

import com.wcgksoft.entity.system.*;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.SysMenuService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.SystemVersionService;
import com.wcgksoft.service.system.UserService;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.mapper.BaseMapper;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MapperCodeGen;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/*.xml")
public class test {


  @Autowired
  private UserService userService;
  @Autowired
  private EnterpriseProjectService enterpriseProjectService;
  @Autowired
  private SystemVersionService systemVersionService;
  @Autowired
  private SysRoleService sysRoleService;
  @Autowired
  private EnterpriseService enterpriseService;
  @Autowired
  private SysMenuService sysMenuService;


  public static void main(String[] args) throws Exception {
    ConnectionSource source = ConnectionSourceHelper.getSimple("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/wcgksoft?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "root");
    DBStyle mysql = new MySqlStyle();
    // sql语句放在classpagth的/mapper 目录下
    SQLLoader loader = new ClasspathLoader("/mapper");
    // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
    UnderlinedNameConversion unc = new UnderlinedNameConversion();
    // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况

    SQLManager sqlManager = new SQLManager(mysql, loader, source, unc, new Interceptor[]{new DebugInterceptor()});
//        sqlManager.genPojoCodeToConsole("bg_admin"); //快速生成，显示到控制台
    // 或者直接生成java文件
    //pojo代码生成配置
    GenConfig config = new GenConfig();
    //忽略表前缀bg_admin默认为BgAdmin，忽略bg为Admin
    config.setIgnorePrefix("bg");
    //dao代码生成配置,无参构造会有默认的模板
    MapperCodeGen mapper = new MapperCodeGen();
    String mapperTemplatePath = mapper.getMapperTemplate();
    //有参构造出传入的是dao生成所在的包，但是没有模板，生成后是空白
    mapper = new MapperCodeGen("com.wcgksoft.dao");
    //把有参构造的模板位置，放入有参构造，这样可以设置dao的生成位置，又不会空白
    mapper.setMapperTemplate(mapperTemplatePath);
    //添加Dao代码生成
    config.codeGens.add(mapper);
    sqlManager.setBaseMapper(BaseMapper.class);
    //生成所有，慎用，会覆盖
    sqlManager.genALL("com.wcgksoft.util", config, null);
  }


  @Test
  @Transactional
  public void test() {
    List<EnterpriseProject> all = enterpriseProjectService.findAll(EnterpriseProject.class);
    EnterpriseProject enterpriseProject = new EnterpriseProject();
    enterpriseProjectService.saveOrUpdate(enterpriseProject);
    for (EnterpriseProject project : all) {
      System.out.println(project.getEnterprise());
    }
    System.out.println(all);
  }

  @Test
  /*版本对菜单*/
 // @javax.transaction.Transactional
  public void bbtocd() {
    //添加
    Set<SysMenu> objects = new HashSet<>();
    for (int i = 0; i < 30; i++) {
      SysMenu menu = new SysMenu();
      menu.setAuthorityName("测" + i);
      menu.setIsMenu(0);
      sysMenuService.save(menu);
      objects.add(menu);
    }
    SystemVersion version = new SystemVersion();
    version.setAddtime(new Date());
    version.setSysMenuSet(objects);
    systemVersionService.save(version);
    //查询
    List<SystemVersion> all = systemVersionService.findAll(SystemVersion.class);
    List<SysRole> all1 = sysRoleService.findAll(SysMenu.class);
    System.out.println("");
  }

  @Test
  /*企业对版本*/
  public void qytobb() {
    Enterprise enterprise = new Enterprise();
    enterprise.setDepartName("测试企业");
    enterprise.setSystemVersion(systemVersionService.findById(1));
    enterpriseService.save(enterprise);
    //取得企业下菜单
    Enterprise byId = enterpriseService.findById(1);
    System.out.println(byId.getSystemVersion());
  }


  @Test
  /*根据企业得到企业下菜单并赋予不角色不同的菜单 此菜的只是本企业下的*/
  public void qyzh() {
    Enterprise byId = enterpriseService.findById(1);
    Set<SysMenu> menuSet =new HashSet<>();
    SysRole role = new SysRole();
    menuSet.addAll(byId.getSystemVersion().getSysMenuSet());
    role.setSysMenuSet(menuSet);
    sysRoleService.save(role);
    List<SysRole> all = sysRoleService.findAll(SysRole.class);
    System.out.println("cao");
    //userService.saveOrUpdate(user);
  }

  @Test
  /*用户关联角色并查询*/
  @Transactional
  public void yhglandse() {
   /* SysUser id = userService.findById(1);
    id.setSysRole(sysRoleService.findById(2));
    userService.saveOrUpdate(id);*/
    SysUser id1 = userService.findById(1);
    System.out.println(id1);
  }



  @Test
  public void sese() {



   /*     List<User> all = userService.findAll(User.class);
        for (User user : all) {
            System.out.println("姓名："+user.getName());
        }*/
    // 返回结果》》  return list;

        /*
        User byId = userService.findById(15);
        String name = byId.getName();
        System.out.println(name);*/

    //return》》  对象 》》object;





      /*  DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(User.class);
        detachedCriteria.add(Restrictions.eq("name","张闯8"));

        List<User> byConditions = userService.findByConditions(detachedCriteria);
        for (User condition : byConditions) {
            System.out.println(condition.getName());
        }*/

        /*
        DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(User.class);
        detachedCriteria.add(Restrictions.eq("name","张闯811"));
        detachedCriteria.add(Restrictions.eq("idCard","123456"));

        List<User> byConditions = userService.findByConditions(detachedCriteria);
        for (User condition : byConditions) {
            System.out.println("name:"+condition.getName());
            System.out.println("idcard:"+condition.getIdCard());
        }*/


    //1。方法需要 传入 DetachedCriteria 》》 DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(xxxxx.class);
    //2.   detachedCriteria.add(Restrictions.eq("name","张闯8"));// detachedCriteria.add 添加条件 （Restrictions.xxx(”你的java类的属性"，“你需要查询的值”)）；
    //3.return List<T>

    // DetachedCriteria detachedCriteria =  DetachedCriteria.forClass(SysUser.class);
      /*  detachedCriteria.add(Restrictions.eq("name","张闯811"));
        detachedCriteria.add(Restrictions.eq("idCard","123456"));*/
    //页数    //每页显示的条数
      /*  List<SysUser> byConditions = userService.findByConditions(detachedCriteria,0,30);
        for (SysUser condition : byConditions) {
            System.out.println("name:"+condition.getName());
            System.out.println("idcard:"+condition.getIdCard());
        }
*/


    //   carouselService.deleteAll("Carousel","4,5,6");

  }


}
